package com.hbnu.net;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author kittychen
 * @since 2025-10-28 16:56
 */
public class TcpServerDemo01 {

    public static void main(String[] args) {
        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(8888);

            Socket socket = serverSocket.accept();  // 获取连接

            InputStream inputStream = socket.getInputStream();

            byte[] bytes = new byte[1024];
            int temp;
            int len = 0;
            while ((temp = inputStream.read()) != -1) {
                bytes[len++] = (byte) temp;
            }

            System.out.println("接收到的数据：" + new String(bytes, 0, len));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
